<?php
/**
 * 案例展示
 */

class CasesController extends Controller {
    private $_model;
    const PAGE_SIZE = 12;

    // lists
    public function actionIndex() {
        $node = Yii::app()->request->getParam('node', 0);

        $criteria = new CDbCriteria(array(
            'condition' => 'status = ' . Options::STATUS_PUBLISHED,
            'order' => 'created DESC',
        ));
        if (isset($_GET['tag'])) {
            $node_name = 'Tag Search';
            $criteria->addSearchCondition('tags', $_GET['tag']);
        } else {
            $criteria->addCondition("node_id = {$node}");
            $node_name = Node::getNodeNameByPk($node);
        }

        $dataProvider = new CActiveDataProvider('UserCase', array(
            'pagination' => array(
                'pageSize' => self::PAGE_SIZE,
                ),
            'criteria' => $criteria,
        ));

        $this->render('index', array(
            'node_name' => $node_name,
            'dataProvider' => $dataProvider,
        ));
    }

    // view details
    public function actionView() {
        $model = $this->loadModel();
        UserCase::model()->updateCounters(array('clicks_count' => 1),
            'id = ' . Yii::app()->request->getParam('id'));

        $this->render('view', array(
            'node_name' => $model->node->name,
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     */
    public function loadModel() {
        if ($this->_model === null) {
            if (isset($_GET['id'])) {
                $condition = 'status = ' . Options::STATUS_PUBLISHED;
                $this->_model = UserCase::model()->findbyPk($_GET['id'], $condition);
            }
            if ($this->_model === null) {
                throw new CHttpException(404, Yii::t('frontend', 'The requested page does not exist.'));
            }
        }
        return $this->_model;
    }
}
